<?php
/**
 * This is the template for generating a controller class file for CRUD feature.
 * The following variables are available in this template:
 * - $this: the CrudCode object
 */
?>
<?php echo "<?php\n"; ?>

class <?php echo $this->controllerClass; ?> extends <?php echo $this->baseControllerClass; ?> {
        public $layout = '//layouts/ace';
        public $headsUp = 'This OK';
<?php 
	$authpath = 'ext.giix-core.giixCrud.templates.default.auth.';
	Yii::app()->controller->renderPartial($authpath . $this->authtype);
?>

	public function actionView() {
		//$this->render('view', array(
			//'model' => $this->loadModel($id, '<?php echo $this->modelClass; ?>'),
		//));



        $id=$_REQUEST["id"];

        if(Yii::app()->request->isAjaxRequest)
        {
        $this->renderPartial('ajax_view',array(
        'model'=>$this->loadModel($id),
        ));

        }
        else
        {
        $this->render('view',array(
        'model'=>$this->loadModel($id),
        ));
        }









	}

	public function actionCreate() {
        $this->headsUp = 'This Create';
		$model = new <?php echo $this->modelClass; ?>;

<?php if ($this->enable_ajax_validation): ?>
		$this->performAjaxValidation($model, '<?php echo $this->class2id($this->modelClass)?>-form');
<?php endif; ?>

		if (isset($_POST['<?php echo $this->modelClass; ?>'])) {
			$model->setAttributes($_POST['<?php echo $this->modelClass; ?>']);
<?php if ($this->hasManyManyRelation($this->modelClass)): ?>
			$relatedData = <?php echo $this->generateGetPostRelatedData($this->modelClass, 4); ?>;
<?php endif; ?>

<?php if ($this->hasManyManyRelation($this->modelClass)): ?>
			if ($model->saveWithRelated($relatedData)) {
<?php else: ?>
			if ($model->save()) {
<?php endif; ?>
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
                    $this -> redirect_message(Yii::t('yii', 'addSucceed'), 'success', 1, Yii::app() -> createUrl('<?php echo strtolower($this->modelClass); ?>'));
			}
		}

		$this->render(
            'create',
            array(
                'model' => $model,
                'headsUp'=> $this->headsUp,
            )
        );
	}

	public function actionUpdate($id) {
        $this->headsUp = 'This Update';
		$model = $this->loadModel($id, '<?php echo $this->modelClass; ?>');

<?php if ($this->enable_ajax_validation): ?>
		$this->performAjaxValidation($model, '<?php echo $this->class2id($this->modelClass)?>-form');
<?php endif; ?>

		if (isset($_POST['<?php echo $this->modelClass; ?>'])) {
			$model->setAttributes($_POST['<?php echo $this->modelClass; ?>']);
<?php if ($this->hasManyManyRelation($this->modelClass)): ?>
			$relatedData = <?php echo $this->generateGetPostRelatedData($this->modelClass, 4); ?>;
<?php endif; ?>

<?php if ($this->hasManyManyRelation($this->modelClass)): ?>
			if ($model->saveWithRelated($relatedData)) {
<?php else: ?>
			if ($model->save()) {
<?php endif; ?>
                $this -> redirect_message(Yii::t('yii', 'updateSucceed'), 'success', 1, Yii::app() -> createUrl('<?php echo strtolower($this->modelClass); ?>'));

			}
		}

		$this->render('update', array(
				'model' => $model,
                'headsUp'=> $this->headsUp,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, '<?php echo $this->modelClass; ?>')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
        $this->headsUp = 'This OK';
        $model = new <?php echo $this->modelClass; ?>('search');
        $model->unsetAttributes();

        if (isset($_GET['<?php echo $this->modelClass; ?>']))
        $model->setAttributes($_GET['<?php echo $this->modelClass; ?>']);

        $this->render('index', array(
        'model' => $model,
        'headsUp'=> $this->headsUp,
        ));
	}



    /**
    * Returns the data model based on the primary key given in the GET variable.
    * If the data model is not found, an HTTP exception will be raised.
    * @param integer the ID of the model to be loaded
    */
    public function loadModel($id)
    {
        $model=<?php echo $this->modelClass; ?>::model()->findByPk($id);
        if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }



}